//
//  PayForViewController.m
//  Experss
//
//  Created by wmwMAC on 17/8/7.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "PayForViewController.h"

#import "PayWayCell.h"

#import "PayResultView.h"//支付结果视图


static NSString *identifier = @"PayWayCell";

@interface PayForViewController ()<UITableViewDelegate,UITableViewDataSource,payResultBackBtnDelegate>
{
    NSArray *_imageA;
    NSArray *_titleA;
    NSArray *_detailTileA;
    NSString *payType;//支付方式
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong)PayResultView *payResultView;


@end

@implementation PayForViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (_payResultView) {
        [_payResultView removeFromSuperview];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"支付";
    
    payType = @"";
    self.view.backgroundColor = BaseColor;
    
    _imageA = @[@"weixin",@"zhifubao",@"yinlian"];
    _titleA = @[@"微信支付",@"支付宝支付",@"银联支付"];
    _detailTileA = @[@"推荐安装微信5.0及以上版本的使用",@"推荐有支付宝账户的用户使用",@"推荐有银行卡的用户使用"];
    
    [self addTableviewUI];
    
}
- (void)addTableviewUI {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 70;
    self.tableView.backgroundColor = BaseColor;
    [self.tableView registerNib:[UINib nibWithNibName:@"PayWayCell" bundle:nil] forCellReuseIdentifier:identifier];
    [self.view addSubview:self.tableView];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titleA.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PayWayCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.selectionStyle = kSeNone;
    
    cell.logoV.image = [UIImage imageNamed:_imageA[indexPath.row]];
    cell.titleLB.text = _titleA[indexPath.row];
    cell.contengLB.text = _detailTileA[indexPath.row];
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        
        return 130;
    }else {
        return 0.01;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = BaseColor;
    
    UIView *backView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 70)];
    backView1.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:backView1];
    UILabel *titleLable = [BasePublicCreatUI createLabelFrame:CGRectMake(20, 0, 100, 70) title:@"订单需支付" textColor:[UIColor blackColor] textAlignment:NSTextAlignmentLeft font:[UIFont systemFontOfSize:16]];
    [backView1 addSubview:titleLable];
    
    UILabel *moneyLable = [BasePublicCreatUI createLabelFrame:CGRectMake(CGRectGetMaxX(titleLable.frame), 0, kScreenWidth-CGRectGetWidth(titleLable.frame)-40, 70) title:[NSString stringWithFormat:@"¥%@",_payMoenyStr] textColor:[UIColor blackColor] textAlignment:NSTextAlignmentRight font:[UIFont systemFontOfSize:16]];
    [backView1 addSubview:moneyLable];
    
    UIView *backView2 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(backView1.frame)+10, kScreenWidth, 50)];
    backView2.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:backView2];
    UILabel *payTypeLable = [BasePublicCreatUI createLabelFrame:CGRectMake(20, 0, kScreenWidth-20, CGRectGetHeight(backView2.frame)) title:@"选择支付方式" textColor:[UIColor lightGrayColor] textAlignment:NSTextAlignmentLeft font:[UIFont systemFontOfSize:14]];
    [backView2 addSubview:payTypeLable];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(backView2.frame)-1, kScreenWidth-40, 1)];
    lineView.backgroundColor = BaseColor;
    [headerView addSubview:lineView];
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 100;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footview = [[UIView alloc] init];
    footview.backgroundColor = BaseColor;
    
    UIButton *payBtn = [BasePublicCreatUI createBtnFrame:CGRectMake(20, 60, kScreenWidth-40, 40) title:@"立即支付" bgImage:@"bg1" selectBgImage:nil image:nil target:self action:@selector(payBtnAction)];
    [payBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    payBtn.layer.cornerRadius = 5;
    payBtn.clipsToBounds = YES;
    [footview addSubview:payBtn];
    
    return footview;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
    payType = @"wechatPay";
    }
    if (indexPath.row == 1) {
        payType = @"alipay";
    }
    if (indexPath.row == 2) {
        payType = @"bankPay";
    }
    
}
-(void)payBtnAction{
    
    NSLog(@"立即支付");
    
    if (payType.length == 0) {
        [MBProgressHUD showText:@"请选择支付方式"];
        return;
    }
    if (_payResultView) {
        [_payResultView removeFromSuperview];
    }
    
    
    NSString *fenMoeny = [NSString stringWithFormat:@"%f",[_payMoenyStr floatValue] *100];
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    NSDictionary *parameDic = @{@"order_no":_orderIdStr,@"total_fee":fenMoeny,@"type":payType,@"actoken":[Md5Apiway getMD5data:saveDic[@"userid"]]};
    [HttpRequest postWithURLString:POST(Pay_NotifyText) parameters:parameDic success:^(id responseObject) {
        
        
    } failure:^(NSError *error) {
        
    }];
    
    
    
    _payResultView = [[PayResultView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    _payResultView.delegate = self;
    _payResultView.imageStr = @"chongzhichengg";
    _payResultView.payResultStr = @"充值成功";
    _payResultView.detailStr = @"已成功充值X元到你的钱包";
    [[UIApplication sharedApplication].keyWindow addSubview:_payResultView];
    
}
- (void)payBackAction:(UIButton *)sender {
    if (_payResultView) {
        [_payResultView removeFromSuperview];
    }
    [self.navigationController popViewControllerAnimated:YES];
}



@end
